﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.AsyncGPUReadbackRequest
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Rendering
{
  /// <summary>
  ///   <para>Represents an asynchronous request for a GPU resource.</para>
  /// </summary>
  [UsedByNativeCode]
  [NativeHeader("Runtime/Graphics/AsyncGPUReadbackManaged.h")]
  [NativeHeader("Runtime/Shaders/ComputeShader.h")]
  [NativeHeader("Runtime/Graphics/Texture.h")]
  public struct AsyncGPUReadbackRequest
  {
    internal IntPtr m_Ptr;
    internal int m_Version;

    /// <summary>
    ///   <para>Triggers an update of the request.</para>
    /// </summary>
    public void Update() => AsyncGPUReadbackRequest.Update_Injected(ref this);

    /// <summary>
    ///   <para>Waits for completion of the request.</para>
    /// </summary>
    public void WaitForCompletion() => AsyncGPUReadbackRequest.WaitForCompletion_Injected(ref this);

    public unsafe NativeArray<T> GetData<T>(int layer = 0) where T : struct
    {
      if (!this.done || this.hasError)
        throw new InvalidOperationException("Cannot access the data as it is not available");
      if (layer < 0 || layer >= this.layerCount)
        throw new ArgumentException(string.Format("Layer index is out of range {0} / {1}", (object) layer, (object) this.layerCount));
      int num = UnsafeUtility.SizeOf<T>();
      NativeArray<T> nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>((void*) this.GetDataRaw(layer), this.layerDataSize / num, Allocator.None);
      NativeArrayUnsafeUtility.SetAtomicSafetyHandle<T>(ref nativeArray, this.GetSafetyHandle());
      return nativeArray;
    }

    /// <summary>
    ///   <para>Checks whether the request has been processed.</para>
    /// </summary>
    public bool done => this.IsDone();

    /// <summary>
    ///   <para>This property is true if the request has encountered an error.</para>
    /// </summary>
    public bool hasError => this.HasError();

    /// <summary>
    ///   <para>Number of layers in the current request.</para>
    /// </summary>
    public int layerCount => this.GetLayerCount();

    /// <summary>
    ///   <para>The size in bytes of one layer of the readback data.</para>
    /// </summary>
    public int layerDataSize => this.GetLayerDataSize();

    /// <summary>
    ///   <para>The width of the requested GPU data.</para>
    /// </summary>
    public int width => this.GetWidth();

    /// <summary>
    ///   <para>When reading data from a ComputeBuffer, height is 1, otherwise, the property takes the value of the requested height from the texture.</para>
    /// </summary>
    public int height => this.GetHeight();

    /// <summary>
    ///   <para>When reading data from a ComputeBuffer, depth is 1, otherwise, the property takes the value of the requested depth from the texture.</para>
    /// </summary>
    public int depth => this.GetDepth();

    private bool IsDone() => AsyncGPUReadbackRequest.IsDone_Injected(ref this);

    private bool HasError() => AsyncGPUReadbackRequest.HasError_Injected(ref this);

    private int GetLayerCount() => AsyncGPUReadbackRequest.GetLayerCount_Injected(ref this);

    private int GetLayerDataSize() => AsyncGPUReadbackRequest.GetLayerDataSize_Injected(ref this);

    private int GetWidth() => AsyncGPUReadbackRequest.GetWidth_Injected(ref this);

    private int GetHeight() => AsyncGPUReadbackRequest.GetHeight_Injected(ref this);

    private int GetDepth() => AsyncGPUReadbackRequest.GetDepth_Injected(ref this);

    internal void CreateSafetyHandle()
    {
      AsyncGPUReadbackRequest.CreateSafetyHandle_Injected(ref this);
    }

    private AtomicSafetyHandle GetSafetyHandle()
    {
      AtomicSafetyHandle ret;
      AsyncGPUReadbackRequest.GetSafetyHandle_Injected(ref this, out ret);
      return ret;
    }

    internal void SetScriptingCallback(Action<AsyncGPUReadbackRequest> callback)
    {
      AsyncGPUReadbackRequest.SetScriptingCallback_Injected(ref this, callback);
    }

    private IntPtr GetDataRaw(int layer)
    {
      return AsyncGPUReadbackRequest.GetDataRaw_Injected(ref this, layer);
    }

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void Update_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void WaitForCompletion_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool IsDone_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool HasError_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetLayerCount_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetLayerDataSize_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetWidth_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetHeight_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetDepth_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void CreateSafetyHandle_Injected(ref AsyncGPUReadbackRequest _unity_self);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void GetSafetyHandle_Injected(
      ref AsyncGPUReadbackRequest _unity_self,
      out AtomicSafetyHandle ret);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern void SetScriptingCallback_Injected(
      ref AsyncGPUReadbackRequest _unity_self,
      Action<AsyncGPUReadbackRequest> callback);

    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern IntPtr GetDataRaw_Injected(
      ref AsyncGPUReadbackRequest _unity_self,
      int layer);
  }
}
